<?php

namespace App\Http\Controllers\Api;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
class ContentController extends Controller
{
    //展示帖子 缺少分页  
    public function  index(Request $request)
    {
        $info  = DB::table('post')
                     ->select('post.title','post.img','post.id','allusers.avatarUrl','allusers.nickname','post.created_at')
                     ->leftJoin('allusers','post.uid','=','allusers.id')
                     ->get();
        
        $sql  = "select p.title,p.img,p.id,a.avatarUrl,a.nickname,p.created_at  from post  p join allusers a on p.uid = a.id"; 
        $info = DB::select($sql);//查询出来每个巴一天收入明细 
        $info = json_encode($info, true);
        $info = json_decode($info, true);
        
            
       foreach($info as &$v){
                
               
                     $v['sum'] = DB::table('post')->leftJoin('post_content','post.id','=','post_content.post_id')->where('post_content.post_id',$v['id'])->count();
                    
                    
               }
            

         
           
             if($info){
                  return $info;
             }else{
                 return $msg = array(
                     'status'=>'200',
                     'message'=>'暂时没有文章',
                 );
             }
  
        
    }
    
    //展示帖子详情  直接返回  如果没有那么就返回报错
    public function indexinfo(Request $request)
    {
        $id = request('id','');
        $info = DB::table('post')
                ->select('post.id','post.title','post.uid','post.content','allusers.nickName','allusers.avatarUrl','post.created_at')
                ->leftJoin('allusers','post.uid','=','allusers.id')
                ->where('post.id',$id)->get();
        
        
        
               $info_info = DB::table('post_content')
                ->select('post_content.id','post_content.uid','post_content.content','allusers.nickName','allusers.avatarUrl','post_content.created_at')
                ->leftJoin('post','post.id','=','post_content.post_id')
                 ->leftJoin('allusers','post_content.uid','=','allusers.id')
                ->where('post_content.post_id',$id)->get();
          
          
                $info_info_info = DB::table('content')
                ->leftJoin('post_content','post_content.id','=','content.content_id')
                 ->leftJoin('allusers','content.uid','=','allusers.id')
                ->where('content.content_id',$id)->get();
               
               
        
         $list = json_decode(json_encode($info),true);
           
          $info_info = json_decode(json_encode($info_info),true);
            
    
          
               
           $info_info_info = json_decode(json_encode($info_info_info),true);
           
           
         if($list){
               $data = array(
                       'data'=>$list,  
                       'huifupl'=>$info_info_info,
                       'huifu'=>$info_info,
                    );
            
                 return $data;
         }else{
               return  $msg = array(
                            'status'=>'000',
                            'message'=>'查看失败',
                    );
         }
       
                    
        
    }
    
    //发帖如果 uid  不存在 那么调用接口 将用户数据存放 
    public  function post(Request $request)
    {
       $data['content'] = request('content');
     //  $data['head_title'] = request('head_title');
       $data['title'] = request('title');
       $data['uid'] = request('uid');   
       $data['created_at'] =  date('Y-m-d H:i:s');
       
       $id = DB::table('post')->insertGetId($data);
       
       if($id){
           return  $msg = array(
                            'status'=>'200',
                            'message'=>'发布成功',
                    );
       }else{
            return  $msg = array(
                            'status'=>'000',
                            'message'=>'发布失败',
                    );
       }
       
    
    }
    
    //评论帖子 
    public  function  postcontent(Request  $request)
    {
        
        $data['post_id'] = Request('post_id');
        $data['uid'] = Request('uid');
        $data['content'] = Request('content');
        $data['created_at'] = date('Y-m-d H:i:s');
        
        $id = DB::table('post_content')->insertGetId($data);
        if($id){
            return $msg = array(
                'status'=>200,
                'message'=>'评论成功',
            );
        }else{
            return $msg = array(
                'status'=>000,
                'message'=>'评论失败',
                
            );
        }
        
    }
    
    //评论回复表
    public  function  content(Request  $request)
    {
        $data['uid'] = request('uid');
        $data['content_id'] = request('content_id');
        $data['content'] = request('content');
        $data['created_at'] = date('Y-m-d H:i:s');
        
        $id = DB::table('content')->insertGetId($data);
        if($id){
            return $msg = array(
                'status'=>200,
                'message'=>'回复成功',
            );
        }else{
            return $msg = array(
                'status'=>000,
                'message'=>'回复失败',
            );
        }
    }
    
    
    public function class_type()
    {
        $data = DB::table('type_class')->where('is_del',0)->get();
        
        $info  = json_decode(json_encode($data),true);
        if(!empty($info)){
              return $info;
        }
        
    }
    
}
